###################################################################################################
# Legislative Term Limits and Polarization ########################################################
# Michael Olson and Jon Rogowski ##################################################################
###################################################################################################

# This script generates results presented in Figure 1A and Table 3 in the text and Figure C.1 and 
# Tables C.3 and C.4 in the supplementary materials. 

###################################################################################################
# working directory and packages ##################################################################
###################################################################################################

  #setwd("./termlimits_polarization_analysisdata")

###################################################################################################
# Data ############################################################################################
###################################################################################################

  pol_dat <- read.csv("pol_dat16.csv")

  pol_dat$leg_expend <- pol_dat$leg_expend/1000
  pol_dat$leg_realsalary <- pol_dat$leg_realsalary/1000
  pol_dat$sess_length <- pol_dat$sess_length/100
  pol_dat$loglegprof <- log(pol_dat$legprofscore)
  pol_dat$logsess <- log(pol_dat$sess_length)
  pol_dat$logsalary <- log(pol_dat$leg_realsalary+1)
  pol_dat$logexpend <- log(pol_dat$leg_expend)
  pol_dat$logbowen1 <- log(pol_dat$bowen_legprof_firstdim+abs(min(pol_dat$bowen_legprof_firstdim))+1)
  pol_dat$logbowen2 <- log(pol_dat$bowen_legprof_seconddim+abs(min(pol_dat$bowen_legprof_seconddim))+1) 
  
###################################################################################################
# Analysis ########################################################################################
###################################################################################################

# Model for Table C.3: Term Limits, Legislative Professionalism, and Party Influence: Unlogged Professionalism Measure
# table reported out from "dime_campaign_finance.R"
   
  base_legprof <- felm(l_diffs~house_term_limit*legprofscore
                  +divided_gov
                  +abs_diff
                  |state+year|0|state
                  ,data=pol_dat)

# Model for Table 3: Term Limits, Legislative Professionalism, and Party Influence: Unlogged Professionalism Measure
# table reported out from "dime_campaign_finance.R"  
  
# Model also for Figure 1A
   
   log_legprof <- felm(l_diffs~house_term_limit*loglegprof
                   +divided_gov+
                   +abs_diff
                   |state+year|0|state
                   ,data=pol_dat)
   
   pdf(paste(output_path,"loglegprof_int.pdf",sep=""),width=8,height=5)
   logout <- continuous_margeff_plot(model=log_legprof,base_coef="house_term_limit",
                           moderating_coef="loglegprof",data=pol_dat,
                           pretty_mod = "Logged Legislative Professionalism")
   print(logout)
   dev.off()
   
# Figure C.1: Effect of Term Limits over Legislative Professionalism Component Parts
  # model also used below for Table C.4

   pieces_log_legprof <- felm(l_diffs~house_term_limit*(logsess+logsalary+logexpend)
                          +divided_gov+
                          +abs_diff
                          |state+year|0|state
                          ,data=pol_dat)
   
   # Figure C.1.A
   
   pdf(paste(output_path,"logsalary_int.pdf",sep=""),width=8,height=5)
   sal <-  continuous_margeff_plot(model=pieces_log_legprof,base_coef="house_term_limit",
                           moderating_coef="logsalary",data=pol_dat,
                           pretty_mod = "Logged Legislative Salary")
   print(sal)
   dev.off()

   # Figure C.1.B
   
   pdf(paste(output_path,"logsesslength_int.pdf",sep=""),width=8,height=5)
   sess <- continuous_margeff_plot(model=pieces_log_legprof,base_coef="house_term_limit",
                           moderating_coef="logsess",data=pol_dat,
                           pretty_mod = "Logged Session Length")
   print(sess)
   dev.off()
   
   # Figure C.1.C

   pdf(paste(output_path,"loglegexp_int.pdf",sep=""),width=8,height=5)
   exp <-    continuous_margeff_plot(model=pieces_log_legprof,base_coef="house_term_limit",
                           moderating_coef="logexpend",data=pol_dat,
                           pretty_mod = "Logged Expenditures per Legislator")
   print(exp)
   dev.off()
   

# Table C.4: Polarization, Term Limits, and Professionalism: Alternative Measures   

   pieces_legprof <- felm(l_diffs~house_term_limit*(sess_length+leg_realsalary+leg_expend)
                   +divided_gov
                   +abs_diff
                   |state+year|0|state
                   ,data=pol_dat)

   logbowen_both_legprof <- felm(l_diffs~house_term_limit*(logbowen1+logbowen2)
                             +divided_gov
                             +abs_diff
                             |state+year|0|state
                             ,data=pol_dat)
   
   bowen_both_legprof <- felm(l_diffs~house_term_limit*(bowen_legprof_firstdim+bowen_legprof_seconddim)
                                 +divided_gov
                              +abs_diff
                                 |state+year|0|state
                                 ,data=pol_dat)
   
   prof_robust_sg <-  stargazer(logbowen_both_legprof,pieces_log_legprof,bowen_both_legprof,pieces_legprof,
                         add.lines = list(c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                          c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                          c("Projected $R^2$",round(summary(logbowen_both_legprof)$P.r.squared,3),round(summary(pieces_log_legprof)$P.r.squared,3)
                                            ,round(summary(bowen_both_legprof)$P.r.squared,3),round(summary(pieces_legprof)$P.r.squared,3))),
                         notes.append = FALSE,notes.label = "",
                         notes="\\parbox[t]{0.65\\textwidth}{\\scriptsize \\textit{Note}: Entries are linear regression coefficients with 
             standard errors clustered on states in parentheses. Session length is measured in hundreds of days, and both
             legislative salary and expenditures per legislature are measured in thousands of dollars. 
             $^{*}$p$<$0.05 (two-tailed test).}",
                         omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "scriptsize",no.space = TRUE,
                         star.char="*",star.cutoffs = 0.05,digits=3,digits.extra=0,
                         dep.var.labels = c("Legislative Polarization"),
                         covariate.labels=c("Term Limits",
                                            "ln(Bowen 1st Dim.)","ln(Bowen 2nd Dim)","ln(Session Length)","ln(Leg. Salary)","ln(Exp. Per Legislator)",
                                            "Bowen 1st Dim.","Bowen 2nd Dim.","Session Length","Leg. Salary","Exp. Per Legislator",
                                            "Divided Gov.","Party Competitiveness",
                                            "Term Limits $\\times$ ln(Bowen 1st Dim.)","Term Limits $\\times$ ln(Bowen 2nd Dim)",
                                            "Term Limits $\\times$ ln(Session Length)","Term Limits $\\times$ ln(Leg. Salary)","Term Limits $\\times$ ln(Exp. Per Legislator)",
                                            "Term Limits $\\times$ Bowen 1st Dim.","Term Limits $\\times$ Bowen 2nd Dim.",
                                            "Term Limits $\\times$ Session Length","Term Limits $\\times$ Leg. Salary","Term Limits $\\times$ Exp. Per Legislator"),
                         label="prof_robust",
                         table.layout ="-ld-#-t-as-n",
                         title="Term Limits and Polarization: Alternative Measures of Legislative Professionalism")
   
   cat(prof_robust_sg, sep = '\n', file = paste(output_path,"prof_robust.tex",sep=""))
   
# reset working directory   
   
   setwd(storewd)
   